from datetime import datetime

from . import db


class Article(db.Model):
    '''文章'''
    __table_name__ = "article"
    id = db.Column(db.Integer, primary_key=True)  # 文章编号
    user_id = db.Column(db.Integer, db.ForeignKey("user.id"))  # 用户id
    title = db.Column(db.String(50), nullable=False)  # 文章标题
    classify = db.Column(db.Integer, db.ForeignKey("classify.id"))  # 文章分类
    content = db.Column(db.Text, nullable=False)  # 文章内容
    status = db.Column(db.Integer, default=0)  # 文章状态
    reason = db.Column(db.String(256))  # 未通过原因
    create_time = db.Column(db.DateTime, default=datetime.now)  # 记录的创建时间
    update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)  # 记录的更新时间
    # collect = db.Column(db.Integer, default=0)  # 收藏数
    clicks = db.Column(db.Integer, default=0)  # 浏览量
    # like = db.Column(db.Integer, default=0)  # 点赞数

    user = db.relationship('User', backref=db.backref('article', lazy='dynamic'))

    def to_dict(self):
        ret = {
            "id": self.id,
            "title": self.title,
            "content": self.content,
            "create_time": self.create_time,
            "collector": self.collector.count(),
            "praise": self.praise.count(),
            "clicks": self.clicks,
            "user_id": self.user_id,
            "user_name": self.user.nick_name,
            "user_ava": self.user.avatar_url
        }
        return ret


class Classify(db.Model):
    """文章分类"""
    __table_name__ = "classify"
    id = db.Column(db.Integer, primary_key=True)  # 分类编号
    name = db.Column(db.String(64), nullable=False)  # 分类名
    create_time = db.Column(db.DateTime, default=datetime.now)  # 记录的创建时间
    update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)  # 记录的更新时间
    status = db.Column(db.Integer, default=1)


class Follow(db.Model):
    """关注"""
    __table_name__ = "follow"
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True)  # 被关注人的id
    focus_id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True)  # 粉丝id


class Collect(db.Model):
    '''收藏'''
    __table_name__ = "collect"
    article_id = db.Column(db.Integer, db.ForeignKey("article.id"), primary_key=True)  # 文章编号
    user_id = db.Column(db.Integer, db.ForeignKey("user.id"), primary_key=True)  # 用户编号


class Like(db.Model):
    '''点赞'''
    __table_name__ = "like"
    article_id = db.Column(db.Integer, db.ForeignKey("article.id"), primary_key=True)  # 文章编号
    user_id = db.Column(db.Integer, db.ForeignKey("user.id"), primary_key=True)  # 用户编号


class User(db.Model):
    """用户"""
    __table_name__ = "user"

    id = db.Column(db.Integer, primary_key=True)  # 用户编号
    nick_name = db.Column(db.String(32), unique=True, nullable=False)  # 用户昵称
    pwd_hash = db.Column(db.String(128), nullable=False)  # 加密的密码
    phone = db.Column(db.String(11), unique=True, nullable=False)  # 手机号
    avatar_url = db.Column(db.String(256))  # 用户头像路径
    create_time = db.Column(db.DateTime, default=datetime.now)  # 注册时间
    login_time = db.Column(db.DateTime, default=datetime.now)  # 最后一次登录时间
    admin = db.Column(db.Boolean, default=False)  # 管理员
    sex = db.Column(db.Enum("1", "2"), default="1")  # 性别（默认男）
    sign = db.Column(db.String(512))  # 用户签名
    likes = db.relationship("Article",  # 点赞
                               secondary=Like.__table__,
                               backref=db.backref('praise', lazy='dynamic'),
                               lazy='dynamic')
    collects = db.relationship("Article",  # 收藏
                               secondary=Collect.__table__,
                               backref=db.backref('collector', lazy='dynamic'),
                               lazy='dynamic')
    fans = db.relationship('User',
                           secondary=Follow.__tablename__,
                           primaryjoin=(id == Follow.user_id),
                           secondaryjoin=(id == Follow.focus_id),
                           backref=db.backref('his_focus', lazy='dynamic'),
                           lazy='dynamic')
    focus = db.relationship('User',
                            secondary=Follow.__tablename__,
                            primaryjoin=(id == Follow.focus_id),
                            secondaryjoin=(id == Follow.user_id),
                            backref=db.backref('his_fans', lazy='dynamic'),
                            lazy='dynamic')
